package com.tim.service.Impl;

import com.tim.dao.Impl.StallUseDaoImpl;
import com.tim.dao.StallUseDao;
import com.tim.pojo.DataResult;
import com.tim.pojo.StallUse;
import com.tim.service.StallUseService;

import java.sql.SQLException;
import java.util.List;

/**
 * 作者：Tim
 * 时间：2021/4/29 14:22
 */
public class StallUserServiceImpl implements StallUseService {
    StallUseDao stallUseDao = new StallUseDaoImpl();

    @Override
    public int deleteStallUse(String id) {
        try {
            int useid= Integer.parseInt(id);
            return stallUseDao.deleteStallUse(useid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int addStallUse(StallUse stallUse) {
        try {
            return stallUseDao.addStallUse(stallUse);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0 ;
    }

    @Override
    public StallUse selectOne(String id) {
        try {
            int useid = Integer.parseInt(id);
            return stallUseDao.selectOne(useid);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateStallUse(StallUse stallUse) {
        try {
            return stallUseDao.updateStallUse(stallUse);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<StallUse> searchStallUse(String carNum) {
        try {
            return stallUseDao.searchStallUse(carNum);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public int deleteAll(String[] ids) {
        //1、直接循环调用删除单个的方法(但是要考虑事务问题。解决方案：动态拼接Sql语句)
        for (String id : ids) {
            int count = deleteStallUse(id);
        }
        return ids.length;
    }

    @Override
    public int addAllStallUse(List<String[]> data) {
        return 0;
    }

    @Override
    public DataResult limit(String currentPage, String pageSize, String carNum) {
        try {
            int p=1;//当前页
            int s=5; //每页条数
            if(currentPage!=null){
                p = Integer.parseInt(currentPage);
            }
            if(pageSize!=null){
                s =Integer.parseInt(pageSize);
            }
            List<StallUse> stallUseList=stallUseDao.limit(p,s,carNum);
            //因为现在使用的是layui的表格渲染，所以返回到前端的数据使用要求的 1、要求code中0表示成功     2、如果开启分页那么必须要返回一个count的参数，表示总条数
            DataResult dataResult = new DataResult(0,"查询成功",stallUseList, stallUseDao.count(carNum));
            return dataResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        DataResult dataResult = new DataResult(100,"数据接口错误");
        return dataResult;
    }
}
